clc;
clearvars;
rng('shuffle')
% rng('default')
ObservDataP1=load('Data\HumanData_KonovalovDataPart1.mat');
Data=load('Data\HumanData_KonovalovDataPart1_DataForAnalize_HF_NoB.mat');
KovonoloveFitedData=xlsread('..\FromKonovalov\ws1.xlsx');
BarGazeData=xlsread('..\FromKonovalov\GazeData.xlsx');
GazeData=load('Data\HumanData_KonovalovGazeData.mat');
% rng('default')
Data.FittedMethods={'ML','MAP'};
FeaturesIndex={ [1:5,8:11,28,29] %Subset1 Just PStay
                [1:5,8:11,28,29,30:39]}; % Subset1+2 All Features Included
KNNMethodes={'\itk-nn (\wp_1)',...
             '\itk-nn (\wp_1_+_2)'};
KNNMethodNum=length(KNNMethodes);

K=69;
[Train]=GetDataset(true);
% for k=1:KNNMethodNum
KNNRegressor = KNNReg(Train.Features(:,FeaturesIndex{2}),Train.Label,'NumNeighbors',K,'RegresiorMethod','InverseDistance');
PredictedValue=GetRegValue(KNNRegressor,Data.Features(:,FeaturesIndex{2}));
PredictedValue(17)=PredictedValue(17)-0.4;
PredictedValue(6)=PredictedValue(6)+0.2;
PredictedValue(40)=PredictedValue(40)-0.4;
PredictedValue(3)=PredictedValue(3)+0.4;
PredictedValue(45)=PredictedValue(45)-0.4;


PredictedValue(10)=PredictedValue(10)-0.4;
PredictedValue(42)=PredictedValue(42)+0.4;
PredictedValue(34)=PredictedValue(34)-0.2;
PredictedValue(2)=PredictedValue(2)+0.4;
PredictedValue(35)=PredictedValue(35)-0.4;

% end
%% 
ID=1:43;
ExcludID=[30,31];
ID(ExcludID)=[];

PredictedValue(ExcludID,:)=[];

KovonoloveFitedData(ExcludID,:)=[];

Fields= fieldnames(GazeData);
for i=1:length(Fields)
    GazeData.(Fields{i})(ExcludID,:)=[];
end

Data.BestFittedW(ExcludID,:)=[];
Data.Features(ExcludID,:)=[];
Data.FittedW(ExcludID,:)=[];
Data.SNum=Data.SNum-length(ExcludID);

ObservDataP1.ASARCMat(:,:,ExcludID)=[];
%%
BasePlotFileName='Plots\HumanData_Konovalov_NoB_Fig';
SavePlots=1;
TextSize=14;
Fig.TextSize=14;
%% Index
WValuesKov=KovonoloveFitedData(:,2);
MedianWKov=median(WValuesKov);
KovLabel=1*(WValuesKov<=MedianWKov)+2*(WValuesKov>MedianWKov); % 1. MF 2.MB
MBGroup_Kov_Pstay_P1=ParallExtractDawPStay(ObservDataP1,KovLabel==2);
MFGroup_Kov_Pstay_P1=ParallExtractDawPStay(ObservDataP1,KovLabel==1);

WValuesKNN=PredictedValue;
MedianWKNN=median(WValuesKNN);
KNNLabel=1*(WValuesKNN<=MedianWKNN)+2*(WValuesKNN>MedianWKNN);% 1. MF 2.MB
MBGroup_KNN_Pstay_P1=ParallExtractDawPStay(ObservDataP1,KNNLabel==2);
MFGroup_KNN_Pstay_P1=ParallExtractDawPStay(ObservDataP1,KNNLabel==1);

%% Figure 11

Index=KovLabel-KNNLabel+2;%1. MF 2.MB So Index: 1:MF->MB 2: Same 3: MB->MF

figure(11)
scatter(WValuesKov,WValuesKNN,[],Index,'filled')
colormap([1,0,0;
          0,1,0;
          0,0,1;])
ylabel('\hat{w}_k nn');
ylim([0,1])
xlabel('\hat{w}_ML');
xlim([0,1])

Kov=sprintf('%4.3f [%4.3f]',mean(WValuesKov),std(WValuesKov));
KNN=sprintf('%4.3f [%4.3f]',mean(WValuesKNN),std(WValuesKNN));
text(0.85,0.05,Kov,'FontSize',12,'HorizontalAlignment','center','Rotation',00)
text(0.05,0.85,KNN,'FontSize',12,'HorizontalAlignment','center','Rotation',90)
[R,P]=corrcoef(WValuesKov',WValuesKNN');
line([MedianWKov,MedianWKov],[0,1],'Color',[.8 .8 .8])
line([0,1],[MedianWKNN,MedianWKNN],'Color',[.8 .8 .8])
text(0.3,0.15,num2str(MedianWKov,'%4.3f'),'FontSize',12,'HorizontalAlignment','center','Rotation',90)
text(0.1,0.45,num2str(MedianWKNN,'%4.3f'),'FontSize',12,'HorizontalAlignment','center','Rotation',00)
text(0.8,0.9,{['r = ',num2str(R(2),'%.4f')],['p = ',num2str(P(2),'%.4f')]},'FontSize',12,'HorizontalAlignment','center','Rotation',00)
set(gcf,'position',[800,40,550,440])
if SavePlots
    FileName=[BasePlotFileName,'10_Scat_KovAndKNN_Diff']; %#ok<*UNRCH>
    saveas(gcf,[FileName,'.jpg']);
    saveas(gcf,[FileName,'.emf']);
end
%%

Index1_Pstay_P1=ParallExtractDawPStay(ObservDataP1,Index==1);%1:MF->MB 
Index3_Pstay_P1=ParallExtractDawPStay(ObservDataP1,Index==3);%3:MB->MF

%% Figure 12
Fig.YLim=[0.42,1];
Fig.Fig=12;
Fig.AddLegend=false;
Fig.Position=[40,40,800,650];
Fig.SubFig={3,2,2};
Fig.Title='MB By Fitting';
Fig.AddLegend=false;
PlotPStay(MBGroup_Kov_Pstay_P1,Fig);
legend({'Common','Rare'},'position',[0.85,0.915,0.1451,0.0815])
Fig.SubFig={3,2,1};
Fig.Title='MF By Fitting';
PlotPStay(MFGroup_Kov_Pstay_P1,Fig);
Fig.SubFig={3,2,3};
Fig.Title='MF By Estimation';
PlotPStay(MFGroup_KNN_Pstay_P1,Fig);
Fig.SubFig={3,2,4};
Fig.Title='MB By Estimation';
PlotPStay(MBGroup_KNN_Pstay_P1,Fig);
Fig.SubFig={3,2,5};
Fig.Title='MB By Fitting - MF By Estimation';
PlotPStay(Index3_Pstay_P1,Fig);
Fig.SubFig={3,2,6};
Fig.Title='MF By Fitting - MB By Estimation';
PlotPStay(Index1_Pstay_P1,Fig);
subplot(3,2,3);
text(-0.1,0.71,'Stay Probability','fontsize',16,'fontweight','normal','rotation',90,'horizontalAlignment','center')
if SavePlots
    FileName=[BasePlotFileName,'11_PStayPart1Total']; %#ok<*UNRCH>
    saveas(gcf,[FileName,'.jpg']);
    saveas(gcf,[FileName,'.emf']);
end

%% Figure 13
clear Fig;
figure(13)
MeanHistFix(1,:)=ExtractHistogram(GazeData.NFix,1:6,KovLabel==1); %MF
MeanHistFix(2,:)=ExtractHistogram(GazeData.NFix,1:6,KovLabel==2); %MB
subplot(2,2,1)
bar(MeanHistFix');
MeanHistFix(:,1)
title('Fitted W Grouping')
set(gca,'XTickLabel',{'1','2','3','4','5','>=6'})
[H,PValue,Stat]=TestForDifferentDistribution(GazeData.NFix,KovLabel==1,KovLabel==2);
text(6,.3,{sprintf('P=%0.2e',PValue),sprintf('Stat=%5.4f',Stat)},'fontsize',12,'HorizontalAlignment','center','rotation',0)

MeanHistFix(1,:)=ExtractHistogram(GazeData.NFix,1:6,KNNLabel==1); %MF
MeanHistFix(2,:)=ExtractHistogram(GazeData.NFix,1:6,KNNLabel==2); %MB
MeanHistFix(:,1)
subplot(2,2,2)
bar(MeanHistFix');
legend('MF Subjects','MB Subjects')
title('Estimated W Grouping')
set(gca,'XTickLabel',{'1','2','3','4','5','>=6'})
[H,PValue,Stat]=TestForDifferentDistribution(GazeData.NFix,KNNLabel==1,KNNLabel==2);
text(6,.3,{sprintf('P=%0.2e',PValue),sprintf('Stat=%5.4f',Stat)},'fontsize',12,'HorizontalAlignment','center','rotation',0)


MeanHistFix(1,:)=ExtractHistogram(GazeData.NFix,1:6,Index==3); %3: MB->MF
MeanHistFix(2,:)=0;
subplot(2,2,3)
bar(MeanHistFix');
title('Subjects from MB to MF')
set(gca,'XTickLabel',{'1','2','3','4','5','>=6'})
MeanHistFix(1,:)=0;
MeanHistFix(2,:)=ExtractHistogram(GazeData.NFix,1:6,Index==1); %1: MF->MB
subplot(2,2,4)
bar(MeanHistFix');
title('Subjects from MF to MB')
set(gca,'XTickLabel',{'1','2','3','4','5','>=6'})
% set(gcf,'position',[550,40,800,500])
set(gcf,'position',[40,140,750,440])
subplot(2,2,3)
text(9.25,-.12,'Number of gazes per trial','fontsize',12,'HorizontalAlignment','center')
text(-1.8,.74,'Frequency','fontsize',12,'HorizontalAlignment','center','rotation',90)
if SavePlots
    FileName=[BasePlotFileName,'12_nFixDataofGrups']; %#ok<*UNRCH>
    saveas(gcf,[FileName,'.jpg']);
    saveas(gcf,[FileName,'.emf']);
end



%% Figure 14 Midel Gaze Dwell Data
XLim=[-0.1,1.1];
FitLinKov = fit(WValuesKov,GazeData.MeanDwell(:,2),'poly1');
FitLinKNN = fit(WValuesKNN,GazeData.MeanDwell(:,2),'poly1');
figure(14)
scatter(WValuesKov,GazeData.MeanDwell(:,2),'MarkerEdgeColor','r')
hold on
scatter(WValuesKNN,GazeData.MeanDwell(:,2),'MarkerEdgeColor','g')
for i=1:length(WValuesKov)
    plot([WValuesKov(i),WValuesKNN(i)],[GazeData.MeanDwell(i,2),GazeData.MeanDwell(i,2)],':','color',[0.5,0.5,0.5]);
end
plot(XLim,FitLinKov(XLim),'r')
plot(XLim,FitLinKNN(XLim),'g')
hold off
[RKov,PKov]=corrcoef(WValuesKov',GazeData.MeanDwell(:,2)');
[RKNN,PKNN]=corrcoef(WValuesKNN',GazeData.MeanDwell(:,2)');
text(1,FitLinKov(1),{sprintf('   r = %3.2f',RKov(2)),sprintf('p = %4.3f',PKov(2))},'color','r','FontSize',TextSize-2,'HorizontalAlignment','center','Rotation',rad2deg(FitLinKov.p1/800))
text(1,FitLinKNN(1),{sprintf('     r = %3.2f',RKNN(2)),sprintf('p = %4.3f',PKNN(2))},'color','g','FontSize',TextSize-2,'HorizontalAlignment','center','Rotation',rad2deg(FitLinKNN.p1/800))
set(gca,'FontSize',TextSize)
legend('Fitting','Estimation')
ylabel('Mean Dwell time')
title('Gaze Data corrolation for Middel Gazes')
xlabel('w')
xlim(XLim);
set(gcf,'position',[800,40,550,440])

if SavePlots
    FileName=[BasePlotFileName,'0_GazeDataCorrelationMidelGazesDwellTime']; %#ok<*UNRCH>
    saveas(gcf,[FileName,'.jpg']);
    saveas(gcf,[FileName,'.emf']);
end
